package com.jiantong.web.open.app.version;

import com.jiantong.version.model.DictionaryVersion;
import com.jiantong.version.service.DictionaryVersionReadService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by mz on 25/8/17.
 *
 */
@Slf4j
@OpenBean
public class DictionaryVersionApis {

    @RpcConsumer
    private DictionaryVersionReadService dictionaryVersionReadService;

    @OpenMethod(
            key = "find.all.dictionary.version.list",
            httpMethods = RequestMethod.GET
    )
    public List<DictionaryVersion> findAll(){

        Response<List<DictionaryVersion>> response = dictionaryVersionReadService.findAll();

        if(!response.isSuccess()){
            log.error("failed to find all dictionary version , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }


    @OpenMethod(
            key = "find.a.dictionary.version.for.app",
            httpMethods = RequestMethod.GET
    )
    public DictionaryVersion findForApp(){

        Response<DictionaryVersion> response = dictionaryVersionReadService.findApp();

        if(!response.isSuccess()){
            log.error("failed to find a dictionary version , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }
}
